package StudyCode.summary.mycollection;

import java.lang.reflect.Array;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class RemovesTheItemsArrayElementsFromTheEndOfTheArray {
	public static <E> E[] truncate(E[] array, int items) {
		if (items == 0)
			return array;
		if (array == null)
			throw new RuntimeException("Array 是空的");
		if (items < 0)
			throw new RuntimeException("数量 < 0");
		if (items >= array.length)
			return (E[]) Array.newInstance(array.getClass()
					.getComponentType(), 0);
		int diff = array.length - items;
		E[] newArray = (E[]) Array.newInstance(array.getClass()
				.getComponentType(), diff);
		System.arraycopy(array, 0, newArray, 0, diff);
		return newArray;
	}
}
